﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using yaTDL.Core.MVVM;
using yaTDL.Core;
using System.Windows.Threading;
using System.Windows.Controls.Primitives;

namespace yaTDL
{
    /// <summary>
    /// Interaction logic for TaskListControl.xaml
    /// </summary>
    public partial class TaskListControl : UserControl
    {


        private GridLengthConverter gridLengthConverter = new GridLengthConverter();
        private double detailsPaneWith_previousValue;
 

        public TaskListControl()
        {
            InitializeComponent();

            this.Loaded += (s, e) => { if (this.DataContext is ProjectTaskListViewModel) (this.DataContext as ProjectTaskListViewModel).PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(TaskListControl_PropertyChanged); };
            this.DataContextChanged += (s, e) => { if (this.DataContext is ProjectTaskListViewModel) { (this.DataContext as ProjectTaskListViewModel).PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(TaskListControl_PropertyChanged); } };
        }


       
        //manually manages the detailsPane's Width that's based on the DetailsPaneVisibility property from the ViewModel. Binding (with a converter doesn't seem to work with a GridSplitter)
        void TaskListControl_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "DetailsPaneVisibility")
            {
                //hides the detailsPane
                if ((sender as ProjectTaskListViewModel).DetailsPaneVisibility == System.Windows.Visibility.Hidden || (sender as ProjectTaskListViewModel).DetailsPaneVisibility == System.Windows.Visibility.Collapsed)
                {
                    this.detailsPane.MinWidth = 0;                                              //sets the MinWidth to 0 so it can be hidden completely
                    detailsPaneWith_previousValue = this.detailsPane.Width.Value;               //saves the current width before hiding the pane
                    this.detailsPane.Width = (GridLength)gridLengthConverter.ConvertFrom(0);    //hides the detailsPane by setting it's with to 0
                }

                //shows the detailsPane                    
                else
                {
                    this.detailsPane.MinWidth = 5;                                                                          //sets the MinWidth to 5 so that the boder is always visible an it is always possible to drag it to increase the pane's width
                    if (detailsPaneWith_previousValue <= 0) detailsPaneWith_previousValue = 200;                            //check if the previously saved width is greater than zero
                    this.detailsPane.Width = (GridLength)gridLengthConverter.ConvertFrom(detailsPaneWith_previousValue);    //sets the detailsPane's width to the previously saved value
                }

            }
        }

   
        
    }









}
